package com.zoweunion.mechanic.controller.api;

import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.entity.WorkHourSumEntity;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.plugins.shiro.JWTUtil;
import com.zoweunion.mechanic.service.app.AppOwnerDataStatisticsService;
import com.zoweunion.mechanic.service.base.UserService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.constants.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("appOwnerDataStatistics")
public class AppOwnerDataStatisticsController extends BaseController{
    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private AppOwnerDataStatisticsService appOwnerDataStatisticsService ;
    @Autowired
    private UserService userService;

    /**
     * 这里边传参时，type为1表示车主，type为2表示服务商
     * @param
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("deviceInfo")
    @ResponseBody
    public ResponseBean deviceInfo(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) throws MyException {

        //这里边传参时，type为1表示车主，type为2表示服务商
        logBefore(logger, "设备信息");
        String oId = getCurrentUser(request).getId();
        String sId = getCurrentUser(request).getS_id();
        reqMap.put("oId", oId);
        reqMap.put("sId", sId);
        getServerSid(sId,reqMap);
        Map<String, Object> deviceInfo = appOwnerDataStatisticsService.deviceInfo(reqMap);
        logAfter(logger);

        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", deviceInfo);
    }

    /**
     * 工单信息(app看板)
     * @param
     * @param request
     * 这里边传参时，type为1表示车主，type为2表示服务商
     * @return
     * @throws MyException
     */
    @PostMapping("orderInfo")
    @ResponseBody
    public ResponseBean orderInfo(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) throws MyException {
        logBefore(logger, "工单信息（车主）");
        String oId = getCurrentUser(request).getId();
        String sId = getCurrentUser(request).getS_id();
        reqMap.put("oId", oId);
        reqMap.put("sId", sId);
        Map<String, Object> orderInfo = appOwnerDataStatisticsService.orderInfo(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", orderInfo);
    }

    /**
     * 工时油耗平均统计
     * 不通过特殊传参字段做判断，仅通过是否传oId和cId判断是否为车主账号或是单一车辆
     */
    @GetMapping("/workFuelAverage")
    public ResponseBean workFuelAverage(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {

        //这边修改是因为app不登陆也需要看到使用车的车辆信息
        String authorization = request.getHeader("Authorization");
        //根据请头中是否带有Authorization信息
        if(authorization!=null) {
            String user_account = JWTUtil.getUsername(authorization);
            User currentUser = userService.getUserByAccount(user_account);
            if(currentUser==null) {
                params.put("s_id", "");
            } else {
                params.put("s_id", getCurrentUser(request).getS_id());
            }
            getServerSid(getCurrentUser(request).getS_id(),params);
        }else {
            params.put("s_id", "");
            getServerSid("b2ee155eed7845809b24e84e54f35060",params);
        }
        Map<String, Object> modelPercent = appOwnerDataStatisticsService.workFuelAverage(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", modelPercent);
    }

    /**
     * 车辆工时占比
     * type为2表示车主，显示车辆名称；type为1表示服务商，显示车主名称
     */
    @GetMapping("/workingHoursPer")
    public ResponseBean workingHoursPer(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        params.put("sId", getCurrentUser(request).getS_id());
        params.put("oId", getCurrentUser(request).getId());
        getServerSid(getCurrentUser(request).getS_id(),params);
        List<WorkHourSumEntity> workingHoursPer = appOwnerDataStatisticsService.workingHoursPer(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", workingHoursPer);
    }

    /**
     * 额度预警(车主)
     * @param
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("quotaWarning")
    @ResponseBody
    public ResponseBean quotaWarning(HttpServletRequest request) throws MyException {
        logBefore(logger, "额度预警");
        String oId = getCurrentUser(request).getId();
        Map<String, Object> quotaWarning = appOwnerDataStatisticsService.quotaWarning(oId);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", quotaWarning);
    }

    /**
     * 维修统计
     * @param
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("maintenanceStatistics")
    @ResponseBody
    public ResponseBean maintenanceStatistics(HttpServletRequest request) throws MyException {
        logBefore(logger, "维修统计");
        Map<String, Object> reqMap = new HashMap<>();
        String sId = getCurrentUser(request).getS_id();
        reqMap.put("sId", sId);
        Map<String, Object> completeOrderPer = appOwnerDataStatisticsService.maintenanceStatistics(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", completeOrderPer);
    }

    /**
     * 额度预警（服务商）
     * @param
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("quotaWarningPer")
    @ResponseBody
    public ResponseBean quotaWarningPer(HttpServletRequest request) throws MyException {
        logBefore(logger, "额度预警");
        String sId = getCurrentUser(request).getS_id();
        Map<String, Object> quotaWarningPer = appOwnerDataStatisticsService.quotaWarningPer(sId);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", quotaWarningPer);
    }

    /**
     * 营收统计
     * 这里边传参时，type为1是日，type为2表示月份
     * @param
     * @return
     * @throws MyException
     */
    @GetMapping("revenueStatistics")
    @ResponseBody
    public ResponseBean revenueStatistics(@RequestParam Map<String, Object> reqMap, HttpServletRequest request) throws MyException {
        logBefore(logger, "营收统计");
        String sId = getCurrentUser(request).getS_id();
        reqMap.put("sId", sId);
        Map<String, Object> revenueStatistics = appOwnerDataStatisticsService.revenueStatistics(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", revenueStatistics);
    }

    /**
     * 车主获取车辆数据
     * @param
     * @return
     */
    @GetMapping("carListByOwner")
    @ResponseBody
    public ResponseBean carListByOwner(@RequestParam Map<String, Object> reqMap) {
        logBefore(logger, "车主获取车辆数据");
        List<Map<String, Object>> carListByOwner = appOwnerDataStatisticsService.carListByOwner(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carListByOwner);
    }


}
